
package com.yimixian.app.ui;

import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.EventBusDomain.PlusMinusIconMessage;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.cart.GoodsItemPresenter;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.common.UMengConstants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.user.UserRegistrationActivity;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.UiUtils;
import com.ymx.sdk.animator.animatorUtils;

import de.greenrobot.event.EventBus;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Interpolator;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.media.Image;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by songzhiyang on 2015/8/22.
 */
public class PlusMinusIcon extends FrameLayout {

    public View linTopMoney;
    public View linBottomMoney; //song 也使用了该View

    public GoodsItem mGoodsItem;
    private GoodsItemPresenter.OnPlusButtonClickListener mOnPlusButtonClickListener;
    public String mCurrentButtonState = "";
    private int mQuantity = 0;
    private RelativeLayout mRLPlusMinusIconHome;
    public String mButtonState = "";
    private boolean mIsCanClick = true;
    public boolean isFirstRunNeedRefresh = false;
    private boolean canBuy = true;
    private LinearLayout ll_go_shangcheng;
    private TranslateAnimation showDialogAnim, hideDialogAnim;
    private boolean isClickCartOrAddButton = false; //是否点击过加入购物车或者加号
    private boolean needShowDialog = false;
    private Handler handler = new Handler();
    private DelayHideRunnable runnable;
    private DelayAnimationListener animationListener;
    public boolean isShowOldPrice = true;

    @InjectView(R.id.quantity_text)
    public TextView mQuantityText;// 下侧view的数量
    @InjectView(R.id.plus_icon)
    public ImageView mPlusButton;// 下侧view的 +
    @InjectView(R.id.minus_icon)
    public ImageView mMinusButton;// 下侧view的 -
    @InjectView(R.id.bar_button)
    TextView mBarButton;// 加入购物车按钮

    /**
     * 专专供于首页
     * @param context
     * @param goodsItem
     * @param currentButtonState
     * @param linTopMoney
     * @param linBottomMoney
     * @param isShowOldPrice
     */
    public PlusMinusIcon(Context context, GoodsItem goodsItem, String currentButtonState,
                         LinearLayout linTopMoney, View linBottomMoney, boolean isShowOldPrice) {
        super(context);
        this.mGoodsItem = goodsItem;
        mCurrentButtonState = currentButtonState;
        this.linTopMoney = linTopMoney;
        this.linBottomMoney = linBottomMoney;
        this.isShowOldPrice = isShowOldPrice;
        initView();
    }

    public PlusMinusIcon(Context context, GoodsItem goodsItem, String currentButtonState,
                         LinearLayout linTopMoney, View linBottomMoney) {
        super(context);
        this.mGoodsItem = goodsItem;
        mCurrentButtonState = currentButtonState;
        this.linTopMoney = linTopMoney;
        this.linBottomMoney = linBottomMoney;
        initView();
    }

    public PlusMinusIcon(Context context, GoodsItem goodsItem, String currentButtonState) {
        super(context);
        this.mGoodsItem = goodsItem;
        mCurrentButtonState = currentButtonState;
        initView();
    }

    public PlusMinusIcon(Context context, AttributeSet attrs, GoodsItem goodsItem,
            String currentButtonState) {
        super(context, attrs);
        this.mGoodsItem = goodsItem;
        mCurrentButtonState = currentButtonState;
        initView();
    }

    public PlusMinusIcon(Context context, AttributeSet attrs, int defStyleAttr,
            GoodsItem goodsItem, String currentButtonState) {
        super(context, attrs, defStyleAttr);
        this.mGoodsItem = goodsItem;
        mCurrentButtonState = currentButtonState;
        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {
//        if (Constants.STATE_HOMEFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)) {
//            View view = LayoutInflater.from(getContext()).inflate(R.layout.plus_minus_button_home,
//                    this);
//            mRLPlusMinusIconHome = (RelativeLayout) view
//                    .findViewById(R.id.rl_plus_minus_buttom_home);
//        } else {
            LayoutInflater.from(getContext()).inflate(R.layout.plus_minus_button, this);
//        }
        ButterKnife.inject(this);
        if (mGoodsItem == null) {
            return;
        }
        initCartGoodsItemView();
        updatePlusMinusIconCount();
    }

    /**
     * 更新数量 同时控制动画
     * 
     * @param checkTypeGift
     * @param goodsId
     */
    public void updateQutity(boolean checkTypeGift, int goodsId) {
        if (checkTypeGift && mOnPlusButtonClickListener != null) {
            if (mCurrentButtonState == Constants.STATE_HOMEFRAGMENT_BUTTON_BROADCAST ||
                    mCurrentButtonState == Constants.STATE_HOMENEWFRAGMENT_BUTTON_BROADCAST ||
                    mCurrentButtonState == Constants.STATE_SESSIONLIST_BUTTON_BROADCAST ||
                mCurrentButtonState == Constants.STATE_DETAILFRAGMENT_BUTTON_BROADCAST ||
                mCurrentButtonState == Constants.STATE_SEARCHFRAGMENT_BUTTON_BROADCAST ||
                    mCurrentButtonState == Constants.STATE_COUDANFRAGMENT_BUTTON_BROADCAST ||
                    mCurrentButtonState == Constants.STATE_HUANGOUFRAGMENT_BUTTON_BROADCAST){
                if (goodsId == mGoodsItem.id){
                    showAddAnimation();
                }
            }
        }
        updatePlusMinusIconCount();
        refreshData();
    }

    public void updateGoodsItem() {
        if(CartManager.getInstance().mCartGoodsItems != null && mGoodsItem != null && CartManager.getInstance().mCartGoodsItems.containsKey(mGoodsItem.id) && CartManager.getInstance().mCartGoodsItems.get(mGoodsItem.id) != null) {
            GoodsItem goodsItem = CartManager.getInstance().mCartGoodsItems.get(mGoodsItem.id);
            if(goodsItem != null) {
                this.mGoodsItem.canNotBuyReason = CartManager.getInstance().mCartGoodsItems.get(mGoodsItem.id).canNotBuyReason;
            }
        }
    }
    /**更新加减号中间商品数量文字*/
    public void updatePlusMinusIconCount() {
        if (mGoodsItem != null && CartManager.getInstance().mCartGoodsItems != null && CartManager.getInstance().mCartGoodsItems.get(mGoodsItem.id) != null) {
            setQuantity(Integer.parseInt(CartManager.getInstance().mCartGoodsItems.get(mGoodsItem.id).count));
        }else{
            setQuantity(0);
        }
    }

    public void showAddAnimation() {
        int[] location = new int[2];
        if (mPlusButton != null) {
            mPlusButton.getLocationInWindow(location);
        }
        if (location[0] != 0 && location[1] != 0) {
            Point point = new Point(location[0], location[1]);
            if (mOnPlusButtonClickListener != null) {
                mOnPlusButtonClickListener.onPlusButtonClick(mPlusButton, point, animBack);
            }
        }
    }

    /**
     * 初始化view 给 + / - button添加点击事件
     */
    protected void initCartGoodsItemView() {
        mBarButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                updateGoodsItem();
                if (showCannotBuyTips()) return;

                mButtonState = Constants.PLUS_BUTTON_CLICKED_BROADCAST;
                String token = DataManager.getInstance().get(CacheKeys.TOKEN);
                if (TextUtils.isEmpty(token)&&mCurrentButtonState==Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST) {
                    checkHasToken();
                    return;
                }
//                if (UiUtils.isFastDoubleClick()) {
//                    return;
//                }
                /*if (!mIsCanClick) {
                    return;
                }*/
                setButtonCanClick(false);
                CartManager.getInstance()
                    .setBroadCastState(mCurrentButtonState);
                UMengPlusIconAgent();
                postIconMessage();
//                setQuantity(CartManager.getInstance().getQuantity(getGoodsItemId()));

                //弹出 "去商城逛逛其他商品"
                if(!CartManager.getInstance().isUserLogin()){
                    showDialog();
                }

                isClickCartOrAddButton = true;

            }
        });

        mPlusButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                updateGoodsItem();
                if (showCannotBuyTips()) return;

                mButtonState = Constants.PLUS_BUTTON_CLICKED_BROADCAST;
                String token = DataManager.getInstance().get(CacheKeys.TOKEN);
                if (TextUtils.isEmpty(token)&&mCurrentButtonState==Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST) {
                    checkHasToken();
                    return;
                }
//                if (UiUtils.isFastDoubleClick()) {
//                    return;
//                }
                /*if (!mIsCanClick) {
                    return;
                }*/

                setButtonCanClick(false);
                CartManager.getInstance()
                        .setBroadCastState(mCurrentButtonState);
//                MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
//                    StatConstants.HOME_GOODVIEW_PLUS_CLICK);
                UMengPlusIconAgent();
                postIconMessage();
//                setQuantity(CartManager.getInstance().getQuantity(getGoodsItemId()));
                if (mCurrentButtonState == Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST) {
                    CartManager.getInstance()
                            .setBroadCastState(Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST);
                    EventBus.getDefault().post(new String(
                            Constants.EVENT_BUS_CART_FRAGMENT_HIDE_FLMASKING + mGoodsItem.id));
                }

                //弹出 "去商城逛逛其他商品"
                if(!CartManager.getInstance().isUserLogin()){
                    showDialog();
                }

                isClickCartOrAddButton = true;

            }
        });
        mMinusButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                mButtonState = Constants.MINUS_BUTTON_CLICKED_BROADCAST;
                String token = DataManager.getInstance().get(CacheKeys.TOKEN);
                if (TextUtils.isEmpty(token)&&mCurrentButtonState==Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST) {
                    checkHasToken();
                    return;
                }
//                if (UiUtils.isFastDoubleClick()) {
//                    return;
//                }
                /*if (!mIsCanClick) {
                    return;
                }*/
                setButtonCanClick(false);
                //                MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                //                    StatConstants.HOME_GOODVIEW_MINUS_CLICK);
                UMengMinusIconAgent();
                postIconMessage();
                //                setQuantity(CartManager.getInstance().getQuantity(getGoodsItemId()));
            }
        });
    }

    private boolean showCannotBuyTips() {
        if(mGoodsItem != null && !canBuy && mGoodsItem.shouldBuyCount < 1 && !TextUtils.isEmpty(mGoodsItem.countZeroReason)) {
            UiUtils.showToast(mGoodsItem.countZeroReason);
            return true;
        }
        if(mGoodsItem != null && !canBuy && mGoodsItem.shouldBuyCount > 0 && !TextUtils.isEmpty(mGoodsItem.canNotBuyReason)) {
            UiUtils.showToast(mGoodsItem.canNotBuyReason);
            return true;
        }
        if(mGoodsItem != null&& !canBuy && mGoodsItem.shouldBuyCount > 0 && !CartManager.getInstance().isUserLogin()){
            UiUtils.showToast("该商品已达到购买上限");
            return true;
        }
        return false;
    }

    private void checkHasToken() {
        setButtonCanClick(true);
        isFirstRunNeedRefresh = true;
        PlusMinusIconMessage message = new PlusMinusIconMessage(PlusMinusIcon.this,
                mGoodsItem.id, false, mCurrentButtonState + "/Login");
        EventBus.getDefault().postSticky(message);
        if (DataManager.getInstance().get(CacheKeys.TOKEN) == null&&mCurrentButtonState==Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST) {
            Intent intent = new Intent(SystemFramework.getInstance().getGlobalContext(),
                    UserRegistrationActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            SystemFramework.getInstance().getGlobalContext().startActivity(intent);
        }
    }

    /**
     * 发送 + / - 被点击的消息
     */
    private void postIconMessage() {
        PlusMinusIconMessage message = new PlusMinusIconMessage(PlusMinusIcon.this,
                mGoodsItem.id, false, mCurrentButtonState);
        EventBus.getDefault().post(message);
    }

    private void refreshData() {
        if (isFirstRunNeedRefresh) {
            EventBus.getDefault().postSticky(new String(Constants.EVENT_BUS_HOME_FRAGMENT_REFRESH));
            isFirstRunNeedRefresh = false;
        }
        if (mCurrentButtonState == Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST
                || mCurrentButtonState == Constants.STATE_DETAILFRAGMENT_BUTTON_BROADCAST) {
            EventBus.getDefault().postSticky(new String(Constants.EVENT_BUS_HOME_FRAGMENT_REFRESH));
        }
        EventBus.getDefault().post(new String(Constants.EVENT_BUS_CART_FRAGMENT_REFRESH));
    }

    /**
     * 设置商品的数量 同时根据商品的数量 设置 + / - Button的状态
     *
     * @param quantity 商品的数量
     */
    public void setQuantity(int quantity) {
        if(!CartManager.getInstance().isUserLogin()&&mGoodsItem != null){
            if(CartManager.offLineCartMap==null) {
                CartManager.offLineCartMap = (HashMap)SharedPreferencesHelper.getObject(SharePreferenceKeys.GOOD_J);
            }else if(CartManager.offLineCartMap.size()>0){
                if (CartManager.offLineCartMap.get(mGoodsItem.id) != null) {
                    quantity = Integer.parseInt(CartManager.offLineCartMap.get(mGoodsItem.id).count);
//                    setQuantity(map.get(mGoodsItem.id));
                }
            }
        }

        if (linTopMoney != null && linBottomMoney != null){
            linTopMoney.setVisibility(View.VISIBLE);
            linBottomMoney.setVisibility(View.VISIBLE);
            if (quantity > 0){
                linTopMoney.setVisibility(View.VISIBLE);
                linBottomMoney.setVisibility(View.INVISIBLE);
            }else{
                linTopMoney.setVisibility(View.INVISIBLE);
                linBottomMoney.setVisibility(View.VISIBLE);
            }
            if (quantity > 0 && linTopMoney.getAlpha() < 1) {//linTopMoney显示
                animatorUtils.alpha(linTopMoney,0f, 1.0f, 300, null).start();
                animatorUtils.alpha(linBottomMoney,1.0f, 0f, 300, null).start();
            }else if (quantity == 0 && linTopMoney.getAlpha() > 0){//linTopMoney隐藏
                animatorUtils.alpha(linTopMoney,1.0f, 0f, 300, null).start();
                animatorUtils.alpha(linBottomMoney, 0f, 1.0f, 300, null).start();
            }
        }

        if(Constants.STATE_HOMEFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState) && linBottomMoney != null) {
            linBottomMoney.setVisibility(quantity > 0 ? View.GONE : View.VISIBLE);
        }

        //没有原价，隐藏
        if(linBottomMoney != null && "noOldPrice".equals(linBottomMoney.getTag())){
            linBottomMoney.setVisibility(View.INVISIBLE);
        }

        mQuantity = quantity;
        canBuy = quantity < mGoodsItem.shouldBuyCount;
        mPlusButton.setClickable(true);
        mPlusButton.setImageResource(canBuy ? R.drawable.icon_plus_selector
                : R.drawable.icon_plus_gray);
        boolean canReduce = quantity > 0;
        mMinusButton.setClickable(canReduce);
        if (canReduce) {
            mMinusButton.setVisibility(View.VISIBLE);
        } else {
            mMinusButton.setVisibility(View.INVISIBLE);
        }
        if(isClickCartOrAddButton){
            needShowDialog = true;
        }
        if (Constants.STATE_HOMEFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)
                || Constants.STATE_HOMENEWFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)
                || Constants.STATE_SESSIONLIST_BUTTON_BROADCAST.equals(mCurrentButtonState)
            ||Constants.STATE_DETAILFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)
            ||Constants.STATE_SEARCHFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)
                ||Constants.STATE_COUDANFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)
                ||Constants.STATE_HUANGOUFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)) {
//            mMinusButton.setImageResource(canReduce ? R.drawable.sellbox_reduce
//                    : R.drawable.cart_minus_unavailable_icon);
            mMinusButton.setImageResource(R.drawable.icon_btn_minus);
            if (quantity == 0) {
                mQuantityText.setVisibility(View.GONE);
                if (Constants.STATE_DETAILFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)) {
                    mPlusButton.setVisibility(View.GONE);
                    if(mBarButton != null) {
                        mBarButton.setVisibility(View.VISIBLE);
                        if(CartManager.getInstance().mIdToGoodsItem != null && mGoodsItem != null && CartManager.getInstance().mIdToGoodsItem.get(mGoodsItem.id) != null ) {
                            if(CartManager.getInstance().mIdToGoodsItem.get(mGoodsItem.id).shouldBuyCount == 0) {
                                mBarButton.setEnabled(true);
                                mBarButton.setBackgroundResource(R.color.cancel_button_color);
                            } else {
                                mBarButton.setEnabled(true);
                                mBarButton.setBackgroundResource(R.color.ymx_orange);
                            }
                        }
                    }
                }
            } else {
                mQuantityText.setVisibility(View.VISIBLE);
                mPlusButton.setVisibility(View.VISIBLE);
                mBarButton.setVisibility(View.GONE);
                if(needShowDialog){
                    showDialog();
                }
            }
        } else {
//            mMinusButton.setImageResource(canReduce ? R.drawable.cart_minus_icon_new
//                    : R.drawable.cart_minus_unavailable_icon);
            mMinusButton.setImageResource(R.drawable.icon_btn_minus);
        }
        mQuantityText.setText(String.valueOf(quantity));
        // controlCartItemMasking();
        // invalidate();

        isClickCartOrAddButton = false;
        needShowDialog = false;

//        if(!isShowOldPrice && linBottomMoney != null)
//            linBottomMoney.setVisibility(View.INVISIBLE);
    }

    private void controlCartItemMasking() {
        if (mCurrentButtonState == Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST) {
            if (mGoodsItem != null) {
                if (getQuantity() == 0) {
                    EventBus.getDefault().post(new String(
                            Constants.EVENT_BUS_CART_FRAGMENT_SHOW_FLMASKING + mGoodsItem.id));
                } else {
                    EventBus.getDefault().post(new String(
                            Constants.EVENT_BUS_CART_FRAGMENT_HIDE_FLMASKING + mGoodsItem.id));
                }
            }
        }
    }

    @Override
    public void invalidate() {
        super.invalidate();
    }

    public int getGoodsItemId() {
        return mGoodsItem.id;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    public void setOnPlusButtonClickListener(
            GoodsItemPresenter.OnPlusButtonClickListener listener) {
        mOnPlusButtonClickListener = listener;
    }

    public int getQuantity() {
        return mQuantity;
    }

    public void updateGoodsItem(GoodsItem goodsItem) {
        this.mGoodsItem = goodsItem;
    }

    public void setButtonCanClick(boolean isCanClick) {
        mIsCanClick = isCanClick;
    }


    private void UMengPlusIconAgent() {
        //各个页面的+按钮点击事件
        if(Constants.STATE_HOMEFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)) {
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                UMengConstants.NEW_CATEGORY_ADDGOOD, hashMap);
        } else if(Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)) {
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                UMengConstants.NEW_CART_ADDGOOD,hashMap);
        } else if(Constants.STATE_DETAILFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)) {
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                UMengConstants.NEW_GOODDETAIL_ADDGOOD,hashMap);
        }else if(Constants.STATE_HOMENEWFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)){
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                    mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_HOME_ADDGOOD,hashMap);
        }else if(Constants.STATE_SESSIONLIST_BUTTON_BROADCAST.equals(mCurrentButtonState)){
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                    mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_SESSION_ADDGOOD,hashMap);
        }else if(Constants.STATE_SEARCHFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)){
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                    mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_SEARCH_ADDGOOD,hashMap);
        }else if(Constants.STATE_COUDANFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)){
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                    mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_ADDON_ADDGOOD,hashMap);
        }else if(Constants.STATE_HUANGOUFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)){
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                    mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_REDEMPTION_ADDGOOD,hashMap);
        }
    }

    private void UMengMinusIconAgent() {
        //各个页面的-按钮点击事件
        if(Constants.STATE_HOMEFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)) {
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                UMengConstants.NEW_CATEGORY_REDUCEGOOD, hashMap);
        } else if(Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)) {
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                UMengConstants.NEW_CART_REDUCEGOOD,hashMap);
        } else if(Constants.STATE_DETAILFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)) {
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                UMengConstants.NEW_GOODDETAIL_REDUCEGOOD,hashMap);
        }else if(Constants.STATE_HOMENEWFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)){
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                    mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_HOME_REDUCEGOOD,hashMap);
        }else if(Constants.STATE_SESSIONLIST_BUTTON_BROADCAST.equals(mCurrentButtonState)){
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                    mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_SESSION_REDUCEGOOD,hashMap);
        }else if(Constants.STATE_SEARCHFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)){
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                    mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_SEARCH_REDUCEGOOD,hashMap);
        }else if(Constants.STATE_COUDANFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)){
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                    mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_ADDON_REDUCEGOOD,hashMap);
        }else if(Constants.STATE_HUANGOUFRAGMENT_BUTTON_BROADCAST.equals(mCurrentButtonState)){
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("ID",
                    mGoodsItem == null ? "" : String.valueOf(mGoodsItem.id));
            MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_REDEMPTION_REDUCEGOOD,hashMap);
        }
    }

    private Bitmap animBack;

    public void setShowBack(Bitmap animBack){
        this.animBack = animBack;
    }

    public Bitmap getAnimBack() {
        return animBack;
    }

    public void setLl_go_shangcheng(LinearLayout ll_go_shangcheng) {
        this.ll_go_shangcheng = ll_go_shangcheng;
    }

    public void showDialog(){
        if(runnable != null)
            runnable.needStop = true;
        runnable = new DelayHideRunnable();
        if(ll_go_shangcheng != null){
            if(ll_go_shangcheng.getVisibility() == View.INVISIBLE){
                ll_go_shangcheng.setVisibility(View.VISIBLE);
                if(animationListener == null){
                    animationListener = new DelayAnimationListener();
                }
                if(showDialogAnim == null){
                    showDialogAnim = new TranslateAnimation(
                            Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0,
                            Animation.RELATIVE_TO_SELF, 2f, Animation.RELATIVE_TO_SELF, 0);
                    showDialogAnim.setDuration(400);
                    showDialogAnim.setInterpolator(new OvershootInterpolator());
                    showDialogAnim.setAnimationListener(animationListener);
                }
                ll_go_shangcheng.startAnimation(showDialogAnim);
            }else{
                handler.postDelayed(runnable, 2000);
            }
        }
    }

    public void hideDialog(){
        if(ll_go_shangcheng != null && ll_go_shangcheng.getVisibility() == View.VISIBLE){
            ll_go_shangcheng.setVisibility(View.INVISIBLE);
            if(hideDialogAnim == null){
                hideDialogAnim = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -2f,
                        Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0);
                hideDialogAnim.setDuration(300);
                hideDialogAnim.setInterpolator(new AnticipateInterpolator());
            }
            ll_go_shangcheng.startAnimation(hideDialogAnim);
        }
    }

    class DelayHideRunnable implements Runnable{

        public boolean needStop = false;

        @Override
        public void run() {
            if(needStop){
                return;
            }
            hideDialog();
        }
    }

    class DelayAnimationListener implements Animation.AnimationListener{

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            handler.postDelayed(runnable, 2000);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    }

}
